

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>optuna.integration._lightgbm_tuner.optimize &mdash; Optuna 1.4.0 文档</title>
  

  
  
    <link rel="shortcut icon" href="../../../../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script src="../../../../_static/jquery.js"></script>
        <script src="../../../../_static/underscore.js"></script>
        <script src="../../../../_static/doctools.js"></script>
        <script src="../../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/css/custom.css" type="text/css" />
    <link rel="index" title="索引" href="../../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> Optuna
          

          
          </a>

          
            
            
              <div class="version">
                1.4.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../installation.html">安装</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../tutorial/index.html">教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../reference/index.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../faq.html">常见问题</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">Optuna</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">模块代码</a> &raquo;</li>
        
          <li><a href="../_lightgbm_tuner.html">optuna.integration._lightgbm_tuner</a> &raquo;</li>
        
      <li>optuna.integration._lightgbm_tuner.optimize</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>optuna.integration._lightgbm_tuner.optimize 源代码</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">abc</span>
<span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">pickle</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Any</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Callable</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Dict</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Generator</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Iterator</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Optional</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Tuple</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Union</span>
<span class="kn">import</span> <span class="nn">warnings</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">tqdm</span>

<span class="kn">import</span> <span class="nn">optuna</span>
<span class="kn">from</span> <span class="nn">optuna._experimental</span> <span class="kn">import</span> <span class="n">experimental</span>
<span class="kn">from</span> <span class="nn">optuna._imports</span> <span class="kn">import</span> <span class="n">try_import</span>
<span class="kn">from</span> <span class="nn">optuna.integration._lightgbm_tuner.alias</span> <span class="kn">import</span> <span class="n">_handling_alias_metrics</span>
<span class="kn">from</span> <span class="nn">optuna.integration._lightgbm_tuner.alias</span> <span class="kn">import</span> <span class="n">_handling_alias_parameters</span>
<span class="kn">from</span> <span class="nn">optuna.study</span> <span class="kn">import</span> <span class="n">Study</span>
<span class="kn">from</span> <span class="nn">optuna.trial</span> <span class="kn">import</span> <span class="n">FrozenTrial</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">type_checking</span>

<span class="k">if</span> <span class="n">type_checking</span><span class="o">.</span><span class="n">TYPE_CHECKING</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">sklearn.model_selection</span> <span class="kn">import</span> <span class="n">BaseCrossValidator</span>  <span class="c1"># NOQA</span>

<span class="k">with</span> <span class="n">try_import</span><span class="p">()</span> <span class="k">as</span> <span class="n">_imports</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">lightgbm</span> <span class="k">as</span> <span class="nn">lgb</span>

    <span class="n">VALID_SET_TYPE</span> <span class="o">=</span> <span class="n">Union</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">lgb</span><span class="o">.</span><span class="n">Dataset</span><span class="p">],</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">lgb</span><span class="o">.</span><span class="n">Dataset</span><span class="p">,</span> <span class="o">...</span><span class="p">],</span> <span class="n">lgb</span><span class="o">.</span><span class="n">Dataset</span><span class="p">]</span>

<span class="c1"># Define key names of `Trial.system_attrs`.</span>
<span class="n">_ELAPSED_SECS_KEY</span> <span class="o">=</span> <span class="s2">&quot;lightgbm_tuner:elapsed_secs&quot;</span>
<span class="n">_AVERAGE_ITERATION_TIME_KEY</span> <span class="o">=</span> <span class="s2">&quot;lightgbm_tuner:average_iteration_time&quot;</span>
<span class="n">_STEP_NAME_KEY</span> <span class="o">=</span> <span class="s2">&quot;lightgbm_tuner:step_name&quot;</span>
<span class="n">_LGBM_PARAMS_KEY</span> <span class="o">=</span> <span class="s2">&quot;lightgbm_tuner:lgbm_params&quot;</span>

<span class="c1"># EPS is used to ensure that a sampled parameter value is in pre-defined value range.</span>
<span class="n">_EPS</span> <span class="o">=</span> <span class="mf">1e-12</span>

<span class="c1"># Default value of tree_depth, used for upper bound of num_leaves.</span>
<span class="n">_DEFAULT_TUNER_TREE_DEPTH</span> <span class="o">=</span> <span class="mi">8</span>

<span class="c1"># Default parameter values described in the official webpage.</span>
<span class="n">_DEFAULT_LIGHTGBM_PARAMETERS</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s2">&quot;lambda_l1&quot;</span><span class="p">:</span> <span class="mf">0.0</span><span class="p">,</span>
    <span class="s2">&quot;lambda_l2&quot;</span><span class="p">:</span> <span class="mf">0.0</span><span class="p">,</span>
    <span class="s2">&quot;num_leaves&quot;</span><span class="p">:</span> <span class="mi">31</span><span class="p">,</span>
    <span class="s2">&quot;feature_fraction&quot;</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">,</span>
    <span class="s2">&quot;bagging_fraction&quot;</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">,</span>
    <span class="s2">&quot;bagging_freq&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
    <span class="s2">&quot;min_child_samples&quot;</span><span class="p">:</span> <span class="mi">20</span><span class="p">,</span>
<span class="p">}</span>

<span class="n">_logger</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">logging</span><span class="o">.</span><span class="n">get_logger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">_BaseTuner</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">lgbm_params</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">lgbm_kwargs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># type: (Dict[str, Any], Dict[str,Any]) -&gt; None</span>

        <span class="c1"># Handling alias metrics.</span>
        <span class="k">if</span> <span class="n">lgbm_params</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">_handling_alias_metrics</span><span class="p">(</span><span class="n">lgbm_params</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span> <span class="o">=</span> <span class="n">lgbm_params</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span> <span class="o">=</span> <span class="n">lgbm_kwargs</span> <span class="ow">or</span> <span class="p">{}</span>

    <span class="k">def</span> <span class="nf">_get_metric_for_objective</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
        <span class="n">metric</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;metric&quot;</span><span class="p">,</span> <span class="s2">&quot;binary_logloss&quot;</span><span class="p">)</span>

        <span class="c1"># todo (smly): This implementation is different logic from the LightGBM&#39;s python bindings.</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">metric</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">str</span><span class="p">:</span>
            <span class="k">pass</span>
        <span class="k">elif</span> <span class="nb">type</span><span class="p">(</span><span class="n">metric</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">list</span><span class="p">:</span>
            <span class="n">metric</span> <span class="o">=</span> <span class="n">metric</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">elif</span> <span class="nb">type</span><span class="p">(</span><span class="n">metric</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">set</span><span class="p">:</span>
            <span class="n">metric</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">metric</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span>
        <span class="n">metric</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_metric_with_eval_at</span><span class="p">(</span><span class="n">metric</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">metric</span>

    <span class="k">def</span> <span class="nf">_get_booster_best_score</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">booster</span><span class="p">):</span>
        <span class="c1"># type: (lgb.Booster) -&gt; float</span>

        <span class="n">metric</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_metric_for_objective</span><span class="p">()</span>
        <span class="n">valid_sets</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;valid_sets&quot;</span><span class="p">)</span>  <span class="c1"># type: Optional[VALID_SET_TYPE]</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;valid_names&quot;</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">[</span><span class="s2">&quot;valid_names&quot;</span><span class="p">])</span> <span class="ow">is</span> <span class="nb">str</span><span class="p">:</span>
                <span class="n">valid_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">[</span><span class="s2">&quot;valid_names&quot;</span><span class="p">]</span>
            <span class="k">elif</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">[</span><span class="s2">&quot;valid_names&quot;</span><span class="p">])</span> <span class="ow">in</span> <span class="p">[</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">]:</span>
                <span class="n">valid_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">[</span><span class="s2">&quot;valid_names&quot;</span><span class="p">][</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">NotImplementedError</span>

        <span class="k">elif</span> <span class="nb">type</span><span class="p">(</span><span class="n">valid_sets</span><span class="p">)</span> <span class="ow">is</span> <span class="n">lgb</span><span class="o">.</span><span class="n">Dataset</span><span class="p">:</span>
            <span class="n">valid_name</span> <span class="o">=</span> <span class="s2">&quot;valid_0&quot;</span>

        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">valid_sets</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">))</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">valid_sets</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">valid_set_idx</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">valid_sets</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
            <span class="n">valid_name</span> <span class="o">=</span> <span class="s2">&quot;valid_</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">valid_set_idx</span><span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span>

        <span class="n">val_score</span> <span class="o">=</span> <span class="n">booster</span><span class="o">.</span><span class="n">best_score</span><span class="p">[</span><span class="n">valid_name</span><span class="p">][</span><span class="n">metric</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">val_score</span>

    <span class="k">def</span> <span class="nf">_metric_with_eval_at</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">metric</span><span class="p">):</span>
        <span class="c1"># type: (str) -&gt; str</span>

        <span class="k">if</span> <span class="n">metric</span> <span class="o">!=</span> <span class="s2">&quot;ndcg&quot;</span> <span class="ow">and</span> <span class="n">metric</span> <span class="o">!=</span> <span class="s2">&quot;map&quot;</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">metric</span>

        <span class="n">eval_at</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;eval_at&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">eval_at</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">eval_at</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2">_at&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">metric</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">eval_at</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">eval_at</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2">_eval_at&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">metric</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">eval_at</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># Set default value of LightGBM.</span>
            <span class="c1"># See https://lightgbm.readthedocs.io/en/latest/Parameters.html#eval_at.</span>
            <span class="n">eval_at</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">]</span>

        <span class="c1"># Optuna can handle only a single metric. Choose first one.</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">eval_at</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">]:</span>
            <span class="k">return</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">@</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">metric</span><span class="p">,</span> <span class="n">eval_at</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">eval_at</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">int</span><span class="p">:</span>
            <span class="k">return</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">@</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">metric</span><span class="p">,</span> <span class="n">eval_at</span><span class="p">)</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s2">&quot;The value of eval_at is expected to be int or a list/tuple of int.&quot;</span>
            <span class="s2">&quot;&#39;</span><span class="si">{}</span><span class="s2">&#39; is specified.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">eval_at</span><span class="p">)</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">higher_is_better</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; bool</span>

        <span class="n">metric_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;metric&quot;</span><span class="p">,</span> <span class="s2">&quot;binary_logloss&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">metric_name</span><span class="o">.</span><span class="n">startswith</span><span class="p">((</span><span class="s2">&quot;auc&quot;</span><span class="p">,</span> <span class="s2">&quot;ndcg&quot;</span><span class="p">,</span> <span class="s2">&quot;map&quot;</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">compare_validation_metrics</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">val_score</span><span class="p">,</span> <span class="n">best_score</span><span class="p">):</span>
        <span class="c1"># type: (float, float) -&gt; bool</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">higher_is_better</span><span class="p">():</span>
            <span class="k">return</span> <span class="n">val_score</span> <span class="o">&gt;</span> <span class="n">best_score</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">val_score</span> <span class="o">&lt;</span> <span class="n">best_score</span>


<span class="k">class</span> <span class="nc">_OptunaObjective</span><span class="p">(</span><span class="n">_BaseTuner</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Objective for hyperparameter-tuning with Optuna.&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">target_param_names</span><span class="p">,</span>  <span class="c1"># type: List[str]</span>
        <span class="n">lgbm_params</span><span class="p">,</span>  <span class="c1"># type: Dict[str, Any]</span>
        <span class="n">train_set</span><span class="p">,</span>  <span class="c1"># type: lgb.Dataset</span>
        <span class="n">lgbm_kwargs</span><span class="p">,</span>  <span class="c1"># type: Dict[str, Any]</span>
        <span class="n">best_score</span><span class="p">,</span>  <span class="c1"># type: float</span>
        <span class="n">step_name</span><span class="p">,</span>  <span class="c1"># type: str</span>
        <span class="n">model_dir</span><span class="p">,</span>  <span class="c1"># type: Optional[str]</span>
        <span class="n">pbar</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[tqdm.tqdm]</span>
    <span class="p">):</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">target_param_names</span> <span class="o">=</span> <span class="n">target_param_names</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pbar</span> <span class="o">=</span> <span class="n">pbar</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span> <span class="o">=</span> <span class="n">lgbm_params</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span> <span class="o">=</span> <span class="n">lgbm_kwargs</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">train_set</span> <span class="o">=</span> <span class="n">train_set</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">trial_count</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">best_score</span> <span class="o">=</span> <span class="n">best_score</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">best_booster_with_trial_number</span> <span class="o">=</span> <span class="kc">None</span>  <span class="c1"># type: Optional[Tuple[lgb.Booster, int]]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">step_name</span> <span class="o">=</span> <span class="n">step_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model_dir</span> <span class="o">=</span> <span class="n">model_dir</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_target_names_supported</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pbar_fmt</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">, val_score: </span><span class="si">{:.6f}</span><span class="s2">&quot;</span>

    <span class="k">def</span> <span class="nf">_check_target_names_supported</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; None</span>

        <span class="n">supported_param_names</span> <span class="o">=</span> <span class="p">[</span>
            <span class="s2">&quot;lambda_l1&quot;</span><span class="p">,</span>
            <span class="s2">&quot;lambda_l2&quot;</span><span class="p">,</span>
            <span class="s2">&quot;num_leaves&quot;</span><span class="p">,</span>
            <span class="s2">&quot;feature_fraction&quot;</span><span class="p">,</span>
            <span class="s2">&quot;bagging_fraction&quot;</span><span class="p">,</span>
            <span class="s2">&quot;bagging_freq&quot;</span><span class="p">,</span>
            <span class="s2">&quot;min_child_samples&quot;</span><span class="p">,</span>
        <span class="p">]</span>
        <span class="k">for</span> <span class="n">target_param_name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_param_names</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">target_param_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">supported_param_names</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;Parameter `</span><span class="si">{}</span><span class="s2">` is not supported for tunning.&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_preprocess</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial</span><span class="p">:</span> <span class="n">optuna</span><span class="o">.</span><span class="n">trial</span><span class="o">.</span><span class="n">Trial</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">pbar</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pbar</span><span class="o">.</span><span class="n">set_description</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">pbar_fmt</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">step_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">best_score</span><span class="p">))</span>

        <span class="k">if</span> <span class="s2">&quot;lambda_l1&quot;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_param_names</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="p">[</span><span class="s2">&quot;lambda_l1&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">suggest_loguniform</span><span class="p">(</span><span class="s2">&quot;lambda_l1&quot;</span><span class="p">,</span> <span class="mf">1e-8</span><span class="p">,</span> <span class="mf">10.0</span><span class="p">)</span>
        <span class="k">if</span> <span class="s2">&quot;lambda_l2&quot;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_param_names</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="p">[</span><span class="s2">&quot;lambda_l2&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">suggest_loguniform</span><span class="p">(</span><span class="s2">&quot;lambda_l2&quot;</span><span class="p">,</span> <span class="mf">1e-8</span><span class="p">,</span> <span class="mf">10.0</span><span class="p">)</span>
        <span class="k">if</span> <span class="s2">&quot;num_leaves&quot;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_param_names</span><span class="p">:</span>
            <span class="n">tree_depth</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;max_depth&quot;</span><span class="p">,</span> <span class="n">_DEFAULT_TUNER_TREE_DEPTH</span><span class="p">)</span>
            <span class="n">max_num_leaves</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">**</span> <span class="n">tree_depth</span> <span class="k">if</span> <span class="n">tree_depth</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="mi">2</span> <span class="o">**</span> <span class="n">_DEFAULT_TUNER_TREE_DEPTH</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="p">[</span><span class="s2">&quot;num_leaves&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">suggest_int</span><span class="p">(</span><span class="s2">&quot;num_leaves&quot;</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">max_num_leaves</span><span class="p">)</span>
        <span class="k">if</span> <span class="s2">&quot;feature_fraction&quot;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_param_names</span><span class="p">:</span>
            <span class="c1"># `GridSampler` is used for sampling feature_fraction value.</span>
            <span class="c1"># The value 1.0 for the hyperparameter is always sampled.</span>
            <span class="n">param_value</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">trial</span><span class="o">.</span><span class="n">suggest_uniform</span><span class="p">(</span><span class="s2">&quot;feature_fraction&quot;</span><span class="p">,</span> <span class="mf">0.4</span><span class="p">,</span> <span class="mf">1.0</span> <span class="o">+</span> <span class="n">_EPS</span><span class="p">),</span> <span class="mf">1.0</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="p">[</span><span class="s2">&quot;feature_fraction&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">param_value</span>
        <span class="k">if</span> <span class="s2">&quot;bagging_fraction&quot;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_param_names</span><span class="p">:</span>
            <span class="c1"># `TPESampler` is used for sampling bagging_fraction value.</span>
            <span class="c1"># The value 1.0 for the hyperparameter might by sampled.</span>
            <span class="n">param_value</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">trial</span><span class="o">.</span><span class="n">suggest_uniform</span><span class="p">(</span><span class="s2">&quot;bagging_fraction&quot;</span><span class="p">,</span> <span class="mf">0.4</span><span class="p">,</span> <span class="mf">1.0</span> <span class="o">+</span> <span class="n">_EPS</span><span class="p">),</span> <span class="mf">1.0</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="p">[</span><span class="s2">&quot;bagging_fraction&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">param_value</span>
        <span class="k">if</span> <span class="s2">&quot;bagging_freq&quot;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_param_names</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="p">[</span><span class="s2">&quot;bagging_freq&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">suggest_int</span><span class="p">(</span><span class="s2">&quot;bagging_freq&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">7</span><span class="p">)</span>
        <span class="k">if</span> <span class="s2">&quot;min_child_samples&quot;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_param_names</span><span class="p">:</span>
            <span class="c1"># `GridSampler` is used for sampling min_child_samples value.</span>
            <span class="c1"># The value 1.0 for the hyperparameter is always sampled.</span>
            <span class="n">param_value</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">trial</span><span class="o">.</span><span class="n">suggest_uniform</span><span class="p">(</span><span class="s2">&quot;min_child_samples&quot;</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">100</span> <span class="o">+</span> <span class="n">_EPS</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="p">[</span><span class="s2">&quot;min_child_samples&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">param_value</span>

    <span class="k">def</span> <span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial</span><span class="p">:</span> <span class="n">optuna</span><span class="o">.</span><span class="n">trial</span><span class="o">.</span><span class="n">Trial</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_preprocess</span><span class="p">(</span><span class="n">trial</span><span class="p">)</span>

        <span class="n">start_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
        <span class="n">booster</span> <span class="o">=</span> <span class="n">lgb</span><span class="o">.</span><span class="n">train</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">train_set</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">)</span>

        <span class="n">val_score</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_booster_best_score</span><span class="p">(</span><span class="n">booster</span><span class="p">)</span>
        <span class="n">elapsed_secs</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="n">start_time</span>
        <span class="n">average_iteration_time</span> <span class="o">=</span> <span class="n">elapsed_secs</span> <span class="o">/</span> <span class="n">booster</span><span class="o">.</span><span class="n">current_iteration</span><span class="p">()</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_dir</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model_dir</span><span class="p">,</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">.pkl&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">trial</span><span class="o">.</span><span class="n">number</span><span class="p">))</span>
            <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="s2">&quot;wb&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fout</span><span class="p">:</span>
                <span class="n">pickle</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">booster</span><span class="p">,</span> <span class="n">fout</span><span class="p">)</span>
            <span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;The booster of trial#</span><span class="si">{}</span><span class="s2"> was saved as </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">trial</span><span class="o">.</span><span class="n">number</span><span class="p">,</span> <span class="n">path</span><span class="p">))</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">compare_validation_metrics</span><span class="p">(</span><span class="n">val_score</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">best_score</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">best_score</span> <span class="o">=</span> <span class="n">val_score</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">best_booster_with_trial_number</span> <span class="o">=</span> <span class="p">(</span><span class="n">booster</span><span class="p">,</span> <span class="n">trial</span><span class="o">.</span><span class="n">number</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_postprocess</span><span class="p">(</span><span class="n">trial</span><span class="p">,</span> <span class="n">elapsed_secs</span><span class="p">,</span> <span class="n">average_iteration_time</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">val_score</span>

    <span class="k">def</span> <span class="nf">_postprocess</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">trial</span><span class="p">:</span> <span class="n">optuna</span><span class="o">.</span><span class="n">trial</span><span class="o">.</span><span class="n">Trial</span><span class="p">,</span> <span class="n">elapsed_secs</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">average_iteration_time</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">pbar</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pbar</span><span class="o">.</span><span class="n">set_description</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">pbar_fmt</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">step_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">best_score</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pbar</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

        <span class="n">trial</span><span class="o">.</span><span class="n">set_system_attr</span><span class="p">(</span><span class="n">_ELAPSED_SECS_KEY</span><span class="p">,</span> <span class="n">elapsed_secs</span><span class="p">)</span>
        <span class="n">trial</span><span class="o">.</span><span class="n">set_system_attr</span><span class="p">(</span><span class="n">_AVERAGE_ITERATION_TIME_KEY</span><span class="p">,</span> <span class="n">average_iteration_time</span><span class="p">)</span>
        <span class="n">trial</span><span class="o">.</span><span class="n">set_system_attr</span><span class="p">(</span><span class="n">_STEP_NAME_KEY</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">step_name</span><span class="p">)</span>
        <span class="n">trial</span><span class="o">.</span><span class="n">set_system_attr</span><span class="p">(</span><span class="n">_LGBM_PARAMS_KEY</span><span class="p">,</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="p">))</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">trial_count</span> <span class="o">+=</span> <span class="mi">1</span>


<span class="k">class</span> <span class="nc">_OptunaObjectiveCV</span><span class="p">(</span><span class="n">_OptunaObjective</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">target_param_names</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">],</span>
        <span class="n">lgbm_params</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">],</span>
        <span class="n">train_set</span><span class="p">:</span> <span class="s2">&quot;lgb.Dataset&quot;</span><span class="p">,</span>
        <span class="n">lgbm_kwargs</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">],</span>
        <span class="n">best_score</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
        <span class="n">step_name</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
        <span class="n">pbar</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">tqdm</span><span class="o">.</span><span class="n">tqdm</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
    <span class="p">):</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">_OptunaObjectiveCV</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
            <span class="n">target_param_names</span><span class="p">,</span>
            <span class="n">lgbm_params</span><span class="p">,</span>
            <span class="n">train_set</span><span class="p">,</span>
            <span class="n">lgbm_kwargs</span><span class="p">,</span>
            <span class="n">best_score</span><span class="p">,</span>
            <span class="n">step_name</span><span class="p">,</span>
            <span class="n">model_dir</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
            <span class="n">pbar</span><span class="o">=</span><span class="n">pbar</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_cv_scores</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cv_results</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">List</span><span class="p">[</span><span class="nb">float</span><span class="p">]])</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="nb">float</span><span class="p">]:</span>

        <span class="n">metric</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_metric_for_objective</span><span class="p">()</span>
        <span class="n">val_scores</span> <span class="o">=</span> <span class="n">cv_results</span><span class="p">[</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2">-mean&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">metric</span><span class="p">)]</span>
        <span class="k">return</span> <span class="n">val_scores</span>

    <span class="k">def</span> <span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial</span><span class="p">:</span> <span class="n">optuna</span><span class="o">.</span><span class="n">trial</span><span class="o">.</span><span class="n">Trial</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_preprocess</span><span class="p">(</span><span class="n">trial</span><span class="p">)</span>

        <span class="n">start_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
        <span class="n">cv_results</span> <span class="o">=</span> <span class="n">lgb</span><span class="o">.</span><span class="n">cv</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">train_set</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">)</span>

        <span class="n">val_scores</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_cv_scores</span><span class="p">(</span><span class="n">cv_results</span><span class="p">)</span>
        <span class="n">val_score</span> <span class="o">=</span> <span class="n">val_scores</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">elapsed_secs</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="n">start_time</span>
        <span class="n">average_iteration_time</span> <span class="o">=</span> <span class="n">elapsed_secs</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">val_scores</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">compare_validation_metrics</span><span class="p">(</span><span class="n">val_score</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">best_score</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">best_score</span> <span class="o">=</span> <span class="n">val_score</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_postprocess</span><span class="p">(</span><span class="n">trial</span><span class="p">,</span> <span class="n">elapsed_secs</span><span class="p">,</span> <span class="n">average_iteration_time</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">val_score</span>


<span class="k">class</span> <span class="nc">_LightGBMBaseTuner</span><span class="p">(</span><span class="n">_BaseTuner</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Base class of LightGBM Tuners.</span>

<span class="sd">    This class has common attributes and method of</span>
<span class="sd">    :class:`~optuna.integration.lightgbm.LightGBMTuner` and</span>
<span class="sd">    :class:`~optuna.integration.lightgbm.LightGBMTunerCV`.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">params</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">],</span>
        <span class="n">train_set</span><span class="p">:</span> <span class="s2">&quot;lgb.Dataset&quot;</span><span class="p">,</span>
        <span class="n">num_boost_round</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1000</span><span class="p">,</span>
        <span class="n">fobj</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Callable</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="n">Any</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">feval</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Callable</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="n">Any</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">feature_name</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">&quot;auto&quot;</span><span class="p">,</span>
        <span class="n">categorical_feature</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">&quot;auto&quot;</span><span class="p">,</span>
        <span class="n">early_stopping_rounds</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">verbose_eval</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Union</span><span class="p">[</span><span class="nb">bool</span><span class="p">,</span> <span class="nb">int</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span>
        <span class="n">callbacks</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">Callable</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="n">Any</span><span class="p">]]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">time_budget</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">sample_size</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">study</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">optuna</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">Study</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">optuna_callbacks</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">Callable</span><span class="p">[[</span><span class="n">Study</span><span class="p">,</span> <span class="n">FrozenTrial</span><span class="p">],</span> <span class="kc">None</span><span class="p">]]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">verbosity</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>

        <span class="n">_imports</span><span class="o">.</span><span class="n">check</span><span class="p">()</span>

        <span class="n">params</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>

        <span class="c1"># Handling alias metrics.</span>
        <span class="n">_handling_alias_metrics</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>

        <span class="n">args</span> <span class="o">=</span> <span class="p">[</span><span class="n">params</span><span class="p">,</span> <span class="n">train_set</span><span class="p">]</span>
        <span class="n">kwargs</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
            <span class="n">num_boost_round</span><span class="o">=</span><span class="n">num_boost_round</span><span class="p">,</span>
            <span class="n">fobj</span><span class="o">=</span><span class="n">fobj</span><span class="p">,</span>
            <span class="n">feval</span><span class="o">=</span><span class="n">feval</span><span class="p">,</span>
            <span class="n">feature_name</span><span class="o">=</span><span class="n">feature_name</span><span class="p">,</span>
            <span class="n">categorical_feature</span><span class="o">=</span><span class="n">categorical_feature</span><span class="p">,</span>
            <span class="n">early_stopping_rounds</span><span class="o">=</span><span class="n">early_stopping_rounds</span><span class="p">,</span>
            <span class="n">verbose_eval</span><span class="o">=</span><span class="n">verbose_eval</span><span class="p">,</span>
            <span class="n">callbacks</span><span class="o">=</span><span class="n">callbacks</span><span class="p">,</span>
            <span class="n">time_budget</span><span class="o">=</span><span class="n">time_budget</span><span class="p">,</span>
            <span class="n">sample_size</span><span class="o">=</span><span class="n">sample_size</span><span class="p">,</span>
            <span class="n">verbosity</span><span class="o">=</span><span class="n">verbosity</span><span class="p">,</span>
        <span class="p">)</span>  <span class="c1"># type: Dict[str, Any]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_parse_args</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_start_time</span> <span class="o">=</span> <span class="kc">None</span>  <span class="c1"># type: Optional[float]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_optuna_callbacks</span> <span class="o">=</span> <span class="n">optuna_callbacks</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_best_params</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="c1"># Set default parameters as best.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_best_params</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">_DEFAULT_LIGHTGBM_PARAMETERS</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">study</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">study</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">create_study</span><span class="p">(</span>
                <span class="n">direction</span><span class="o">=</span><span class="s2">&quot;maximize&quot;</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">higher_is_better</span><span class="p">()</span> <span class="k">else</span> <span class="s2">&quot;minimize&quot;</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">study</span> <span class="o">=</span> <span class="n">study</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">higher_is_better</span><span class="p">():</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">direction</span> <span class="o">!=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">StudyDirection</span><span class="o">.</span><span class="n">MAXIMIZE</span><span class="p">:</span>
                <span class="n">metric_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;metric&quot;</span><span class="p">,</span> <span class="s2">&quot;binary_logloss&quot;</span><span class="p">)</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                    <span class="s2">&quot;Study direction is inconsistent with the metric </span><span class="si">{}</span><span class="s2">. &quot;</span>
                    <span class="s2">&quot;Please set &#39;maximize&#39; as the direction.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">metric_name</span><span class="p">)</span>
                <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">direction</span> <span class="o">!=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">StudyDirection</span><span class="o">.</span><span class="n">MINIMIZE</span><span class="p">:</span>
                <span class="n">metric_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;metric&quot;</span><span class="p">,</span> <span class="s2">&quot;binary_logloss&quot;</span><span class="p">)</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                    <span class="s2">&quot;Study direction is inconsistent with the metric </span><span class="si">{}</span><span class="s2">. &quot;</span>
                    <span class="s2">&quot;Please set &#39;minimize&#39; as the direction.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">metric_name</span><span class="p">)</span>
                <span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">best_score</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Return the score of the best booster.&quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">best_value</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="c1"># Return the default score because no trials have completed.</span>
            <span class="k">return</span> <span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">inf</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">higher_is_better</span><span class="p">()</span> <span class="k">else</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">best_params</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;Return parameters of the best booster.&quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">best_trial</span><span class="o">.</span><span class="n">system_attrs</span><span class="p">[</span><span class="n">_LGBM_PARAMS_KEY</span><span class="p">])</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="c1"># Return the default score because no trials have completed.</span>
            <span class="n">params</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">_DEFAULT_LIGHTGBM_PARAMETERS</span><span class="p">)</span>
            <span class="c1"># self.lgbm_params may contain parameters given by users.</span>
            <span class="n">params</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">params</span>

    <span class="k">def</span> <span class="nf">_parse_args</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">:</span> <span class="n">Any</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">:</span> <span class="n">Any</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">auto_options</span> <span class="o">=</span> <span class="p">{</span>
            <span class="n">option_name</span><span class="p">:</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">option_name</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">option_name</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;time_budget&quot;</span><span class="p">,</span> <span class="s2">&quot;sample_size&quot;</span><span class="p">,</span> <span class="s2">&quot;verbosity&quot;</span><span class="p">]</span>
        <span class="p">}</span>

        <span class="c1"># Split options.</span>
        <span class="k">for</span> <span class="n">option_name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">auto_options</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">option_name</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
                <span class="k">del</span> <span class="n">kwargs</span><span class="p">[</span><span class="n">option_name</span><span class="p">]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span> <span class="o">=</span> <span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">train_set</span> <span class="o">=</span> <span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">train_subset</span> <span class="o">=</span> <span class="kc">None</span>  <span class="c1"># Use for sampling.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span> <span class="o">=</span> <span class="n">kwargs</span>

    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Perform the hyperparameter-tuning with given parameters.&quot;&quot;&quot;</span>
        <span class="c1"># Suppress log messages.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">auto_options</span><span class="p">[</span><span class="s2">&quot;verbosity&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">optuna</span><span class="o">.</span><span class="n">logging</span><span class="o">.</span><span class="n">disable_default_handler</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="p">[</span><span class="s2">&quot;verbose&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="p">[</span><span class="s2">&quot;seed&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">111</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">[</span><span class="s2">&quot;verbose_eval&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="c1"># Handling aliases.</span>
        <span class="n">_handling_alias_parameters</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="p">)</span>

        <span class="c1"># Sampling.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sample_train_set</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">tune_feature_fraction</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tune_num_leaves</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tune_bagging</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tune_feature_fraction_stage2</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tune_regularization_factors</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tune_min_data_in_leaf</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">sample_train_set</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Make subset of `self.train_set` Dataset object.&quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">auto_options</span><span class="p">[</span><span class="s2">&quot;sample_size&quot;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">train_set</span><span class="o">.</span><span class="n">construct</span><span class="p">()</span>
        <span class="n">n_train_instance</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">train_set</span><span class="o">.</span><span class="n">get_label</span><span class="p">()</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">n_train_instance</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">auto_options</span><span class="p">[</span><span class="s2">&quot;sample_size&quot;</span><span class="p">]:</span>
            <span class="n">offset</span> <span class="o">=</span> <span class="n">n_train_instance</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">auto_options</span><span class="p">[</span><span class="s2">&quot;sample_size&quot;</span><span class="p">]</span>
            <span class="n">idx_list</span> <span class="o">=</span> <span class="n">offset</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">auto_options</span><span class="p">[</span><span class="s2">&quot;sample_size&quot;</span><span class="p">])</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">train_subset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">train_set</span><span class="o">.</span><span class="n">subset</span><span class="p">(</span><span class="n">idx_list</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tune_feature_fraction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n_trials</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">7</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">param_name</span> <span class="o">=</span> <span class="s2">&quot;feature_fraction&quot;</span>
        <span class="n">param_values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mf">0.4</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="n">n_trials</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>

        <span class="c1"># TODO(toshihikoyanase): Remove catch_warnings after GridSampler becomes non-experimental.</span>
        <span class="k">with</span> <span class="n">warnings</span><span class="o">.</span><span class="n">catch_warnings</span><span class="p">():</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s2">&quot;ignore&quot;</span><span class="p">,</span> <span class="n">category</span><span class="o">=</span><span class="n">optuna</span><span class="o">.</span><span class="n">exceptions</span><span class="o">.</span><span class="n">ExperimentalWarning</span><span class="p">)</span>
            <span class="n">sampler</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">samplers</span><span class="o">.</span><span class="n">GridSampler</span><span class="p">({</span><span class="n">param_name</span><span class="p">:</span> <span class="n">param_values</span><span class="p">})</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_tune_params</span><span class="p">([</span><span class="n">param_name</span><span class="p">],</span> <span class="nb">len</span><span class="p">(</span><span class="n">param_values</span><span class="p">),</span> <span class="n">sampler</span><span class="p">,</span> <span class="s2">&quot;feature_fraction&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tune_num_leaves</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n_trials</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">20</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_tune_params</span><span class="p">([</span><span class="s2">&quot;num_leaves&quot;</span><span class="p">],</span> <span class="n">n_trials</span><span class="p">,</span> <span class="n">optuna</span><span class="o">.</span><span class="n">samplers</span><span class="o">.</span><span class="n">TPESampler</span><span class="p">(),</span> <span class="s2">&quot;num_leaves&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tune_bagging</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n_trials</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">10</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_tune_params</span><span class="p">(</span>
            <span class="p">[</span><span class="s2">&quot;bagging_fraction&quot;</span><span class="p">,</span> <span class="s2">&quot;bagging_freq&quot;</span><span class="p">],</span> <span class="n">n_trials</span><span class="p">,</span> <span class="n">optuna</span><span class="o">.</span><span class="n">samplers</span><span class="o">.</span><span class="n">TPESampler</span><span class="p">(),</span> <span class="s2">&quot;bagging&quot;</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">tune_feature_fraction_stage2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n_trials</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">6</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">param_name</span> <span class="o">=</span> <span class="s2">&quot;feature_fraction&quot;</span>
        <span class="n">best_feature_fraction</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">best_params</span><span class="p">[</span><span class="n">param_name</span><span class="p">]</span>
        <span class="n">param_values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span>
            <span class="n">best_feature_fraction</span> <span class="o">-</span> <span class="mf">0.08</span><span class="p">,</span> <span class="n">best_feature_fraction</span> <span class="o">+</span> <span class="mf">0.08</span><span class="p">,</span> <span class="n">n_trials</span>
        <span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
        <span class="n">param_values</span> <span class="o">=</span> <span class="p">[</span><span class="n">val</span> <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">param_values</span> <span class="k">if</span> <span class="n">val</span> <span class="o">&gt;=</span> <span class="mf">0.4</span> <span class="ow">and</span> <span class="n">val</span> <span class="o">&lt;=</span> <span class="mf">1.0</span><span class="p">]</span>

        <span class="c1"># TODO(toshihikoyanase): Remove catch_warnings after GridSampler becomes non-experimental.</span>
        <span class="k">with</span> <span class="n">warnings</span><span class="o">.</span><span class="n">catch_warnings</span><span class="p">():</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s2">&quot;ignore&quot;</span><span class="p">,</span> <span class="n">category</span><span class="o">=</span><span class="n">optuna</span><span class="o">.</span><span class="n">exceptions</span><span class="o">.</span><span class="n">ExperimentalWarning</span><span class="p">)</span>
            <span class="n">sampler</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">samplers</span><span class="o">.</span><span class="n">GridSampler</span><span class="p">({</span><span class="n">param_name</span><span class="p">:</span> <span class="n">param_values</span><span class="p">})</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_tune_params</span><span class="p">([</span><span class="n">param_name</span><span class="p">],</span> <span class="nb">len</span><span class="p">(</span><span class="n">param_values</span><span class="p">),</span> <span class="n">sampler</span><span class="p">,</span> <span class="s2">&quot;feature_fraction_stage2&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tune_regularization_factors</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n_trials</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">20</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_tune_params</span><span class="p">(</span>
            <span class="p">[</span><span class="s2">&quot;lambda_l1&quot;</span><span class="p">,</span> <span class="s2">&quot;lambda_l2&quot;</span><span class="p">],</span>
            <span class="n">n_trials</span><span class="p">,</span>
            <span class="n">optuna</span><span class="o">.</span><span class="n">samplers</span><span class="o">.</span><span class="n">TPESampler</span><span class="p">(),</span>
            <span class="s2">&quot;regularization_factors&quot;</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">tune_min_data_in_leaf</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">param_name</span> <span class="o">=</span> <span class="s2">&quot;min_child_samples&quot;</span>
        <span class="n">param_values</span> <span class="o">=</span> <span class="p">[</span><span class="mi">5</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">25</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">100</span><span class="p">]</span>

        <span class="c1"># TODO(toshihikoyanase): Remove catch_warnings after GridSampler becomes non-experimental.</span>
        <span class="k">with</span> <span class="n">warnings</span><span class="o">.</span><span class="n">catch_warnings</span><span class="p">():</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s2">&quot;ignore&quot;</span><span class="p">,</span> <span class="n">category</span><span class="o">=</span><span class="n">optuna</span><span class="o">.</span><span class="n">exceptions</span><span class="o">.</span><span class="n">ExperimentalWarning</span><span class="p">)</span>
            <span class="n">sampler</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">samplers</span><span class="o">.</span><span class="n">GridSampler</span><span class="p">({</span><span class="n">param_name</span><span class="p">:</span> <span class="n">param_values</span><span class="p">})</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_tune_params</span><span class="p">([</span><span class="n">param_name</span><span class="p">],</span> <span class="nb">len</span><span class="p">(</span><span class="n">param_values</span><span class="p">),</span> <span class="n">sampler</span><span class="p">,</span> <span class="s2">&quot;min_data_in_leaf&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_tune_params</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">target_param_names</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">],</span>
        <span class="n">n_trials</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
        <span class="n">sampler</span><span class="p">:</span> <span class="n">optuna</span><span class="o">.</span><span class="n">samplers</span><span class="o">.</span><span class="n">BaseSampler</span><span class="p">,</span>
        <span class="n">step_name</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">_OptunaObjective</span><span class="p">:</span>
        <span class="n">pbar</span> <span class="o">=</span> <span class="n">tqdm</span><span class="o">.</span><span class="n">tqdm</span><span class="p">(</span><span class="n">total</span><span class="o">=</span><span class="n">n_trials</span><span class="p">,</span> <span class="n">ascii</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

        <span class="c1"># Set current best parameters.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">best_params</span><span class="p">)</span>

        <span class="n">train_set</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">train_set</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">train_subset</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">train_set</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">train_subset</span>

        <span class="n">objective</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_objective</span><span class="p">(</span><span class="n">target_param_names</span><span class="p">,</span> <span class="n">train_set</span><span class="p">,</span> <span class="n">step_name</span><span class="p">,</span> <span class="n">pbar</span><span class="p">)</span>

        <span class="n">study</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_stepwise_study</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="p">,</span> <span class="n">step_name</span><span class="p">)</span>
        <span class="n">study</span><span class="o">.</span><span class="n">sampler</span> <span class="o">=</span> <span class="n">sampler</span>

        <span class="n">complete_trials</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">t</span>
            <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">study</span><span class="o">.</span><span class="n">trials</span>
            <span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">state</span> <span class="ow">in</span> <span class="p">(</span><span class="n">optuna</span><span class="o">.</span><span class="n">trial</span><span class="o">.</span><span class="n">TrialState</span><span class="o">.</span><span class="n">COMPLETE</span><span class="p">,</span> <span class="n">optuna</span><span class="o">.</span><span class="n">trial</span><span class="o">.</span><span class="n">TrialState</span><span class="o">.</span><span class="n">PRUNED</span><span class="p">)</span>
        <span class="p">]</span>
        <span class="n">_n_trials</span> <span class="o">=</span> <span class="n">n_trials</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">complete_trials</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_start_time</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_start_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">auto_options</span><span class="p">[</span><span class="s2">&quot;time_budget&quot;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">_timeout</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">auto_options</span><span class="p">[</span><span class="s2">&quot;time_budget&quot;</span><span class="p">]</span> <span class="o">-</span> <span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">_start_time</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">_timeout</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">_n_trials</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">study</span><span class="o">.</span><span class="n">optimize</span><span class="p">(</span>
                    <span class="n">objective</span><span class="p">,</span>
                    <span class="n">n_trials</span><span class="o">=</span><span class="n">_n_trials</span><span class="p">,</span>
                    <span class="n">timeout</span><span class="o">=</span><span class="n">_timeout</span><span class="p">,</span>
                    <span class="n">catch</span><span class="o">=</span><span class="p">(),</span>
                    <span class="n">callbacks</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_optuna_callbacks</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                <span class="c1"># ValueError is raised by GridSampler when all combinations were examined.</span>
                <span class="c1"># TODO(toshihikoyanase): Remove this try-except after Study.stop is implemented.</span>
                <span class="k">pass</span>

        <span class="n">pbar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="k">del</span> <span class="n">pbar</span>

        <span class="k">return</span> <span class="n">objective</span>

    <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
    <span class="k">def</span> <span class="nf">_create_objective</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">target_param_names</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">],</span>
        <span class="n">train_set</span><span class="p">:</span> <span class="s2">&quot;lgb.Dataset&quot;</span><span class="p">,</span>
        <span class="n">step_name</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
        <span class="n">pbar</span><span class="p">:</span> <span class="n">tqdm</span><span class="o">.</span><span class="n">tqdm</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">_OptunaObjective</span><span class="p">:</span>

        <span class="k">raise</span> <span class="ne">NotImplementedError</span>

    <span class="k">def</span> <span class="nf">_create_stepwise_study</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">:</span> <span class="s2">&quot;optuna.study.Study&quot;</span><span class="p">,</span> <span class="n">step_name</span><span class="p">:</span> <span class="nb">str</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;optuna.study.Study&quot;</span><span class="p">:</span>

        <span class="c1"># This class is assumed to be passed to a sampler and a pruner corresponding to the step.</span>
        <span class="k">class</span> <span class="nc">_StepwiseStudy</span><span class="p">(</span><span class="n">optuna</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">Study</span><span class="p">):</span>
            <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">:</span> <span class="n">optuna</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">Study</span><span class="p">,</span> <span class="n">step_name</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>

                <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
                    <span class="n">study_name</span><span class="o">=</span><span class="n">study</span><span class="o">.</span><span class="n">study_name</span><span class="p">,</span>
                    <span class="n">storage</span><span class="o">=</span><span class="n">study</span><span class="o">.</span><span class="n">_storage</span><span class="p">,</span>
                    <span class="n">sampler</span><span class="o">=</span><span class="n">study</span><span class="o">.</span><span class="n">sampler</span><span class="p">,</span>
                    <span class="n">pruner</span><span class="o">=</span><span class="n">study</span><span class="o">.</span><span class="n">pruner</span><span class="p">,</span>
                <span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_step_name</span> <span class="o">=</span> <span class="n">step_name</span>

            <span class="k">def</span> <span class="nf">get_trials</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">deepcopy</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">optuna</span><span class="o">.</span><span class="n">trial</span><span class="o">.</span><span class="n">FrozenTrial</span><span class="p">]:</span>

                <span class="n">trials</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_trials</span><span class="p">(</span><span class="n">deepcopy</span><span class="o">=</span><span class="n">deepcopy</span><span class="p">)</span>
                <span class="k">return</span> <span class="p">[</span><span class="n">t</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">trials</span> <span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">system_attrs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">_STEP_NAME_KEY</span><span class="p">)</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_step_name</span><span class="p">]</span>

            <span class="nd">@property</span>
            <span class="k">def</span> <span class="nf">best_trial</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">optuna</span><span class="o">.</span><span class="n">trial</span><span class="o">.</span><span class="n">FrozenTrial</span><span class="p">:</span>
                <span class="sd">&quot;&quot;&quot;Return the best trial in the study.</span>

<span class="sd">                Returns:</span>
<span class="sd">                    A :class:`~optuna.trial.FrozenTrial` object of the best trial.</span>
<span class="sd">                &quot;&quot;&quot;</span>

                <span class="n">trials</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_trials</span><span class="p">(</span><span class="n">deepcopy</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
                <span class="n">trials</span> <span class="o">=</span> <span class="p">[</span><span class="n">t</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">trials</span> <span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">state</span> <span class="ow">is</span> <span class="n">optuna</span><span class="o">.</span><span class="n">trial</span><span class="o">.</span><span class="n">TrialState</span><span class="o">.</span><span class="n">COMPLETE</span><span class="p">]</span>

                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">trials</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;No trials are completed yet.&quot;</span><span class="p">)</span>

                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">direction</span> <span class="o">==</span> <span class="n">optuna</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">StudyDirection</span><span class="o">.</span><span class="n">MINIMIZE</span><span class="p">:</span>
                    <span class="n">best_trial</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">trials</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">best_trial</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">trials</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
                <span class="k">return</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">best_trial</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">_StepwiseStudy</span><span class="p">(</span><span class="n">study</span><span class="p">,</span> <span class="n">step_name</span><span class="p">)</span>


<div class="viewcode-block" id="LightGBMTuner"><a class="viewcode-back" href="../../../../reference/integration.html#optuna.integration.lightgbm.LightGBMTuner">[文档]</a><span class="nd">@experimental</span><span class="p">(</span><span class="s2">&quot;1.5.0&quot;</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">LightGBMTuner</span><span class="p">(</span><span class="n">_LightGBMBaseTuner</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Hyperparameter tuner for LightGBM.</span>

<span class="sd">    It optimizes the following hyperparameters in a stepwise manner:</span>
<span class="sd">    ``lambda_l1``, ``lambda_l2``, ``num_leaves``, ``feature_fraction``, ``bagging_fraction``,</span>
<span class="sd">    ``bagging_freq`` and ``min_child_samples``.</span>

<span class="sd">    You can find the details of the algorithm and benchmark results in `this blog article &lt;https:/</span>
<span class="sd">    /medium.com/optuna/lightgbm-tuner-new-optuna-integration-for-hyperparameter-optimization-8b709</span>
<span class="sd">    5e99258&gt;`_ by `Kohei Ozaki &lt;https://www.kaggle.com/confirm&gt;`_, a Kaggle Grandmaster.</span>

<span class="sd">    Arguments and keyword arguments for `lightgbm.train()</span>
<span class="sd">    &lt;https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.train.html&gt;`_ can be passed.</span>
<span class="sd">    The arguments that only :class:`~optuna.integration.lightgbm.LightGBMTuner` has are</span>
<span class="sd">    listed below:</span>

<span class="sd">    Args:</span>
<span class="sd">        time_budget:</span>
<span class="sd">            A time budget for parameter tuning in seconds.</span>

<span class="sd">        study:</span>
<span class="sd">            A :class:`~optuna.study.Study` instance to store optimization results. The</span>
<span class="sd">            :class:`~optuna.trial.Trial` instances in it has the following user attributes:</span>
<span class="sd">            ``elapsed_secs`` is the elapsed time since the optimization starts.</span>
<span class="sd">            ``average_iteration_time`` is the average time of iteration to train the booster</span>
<span class="sd">            model in the trial. ``lgbm_params`` is a JSON-serialized dictionary of LightGBM</span>
<span class="sd">            parameters used in the trial.</span>

<span class="sd">        optuna_callbacks:</span>
<span class="sd">            List of Optuna callback functions that are invoked at the end of each trial.</span>
<span class="sd">            Each function must accept two parameters with the following types in this order:</span>
<span class="sd">            :class:`~optuna.study.Study` and :class:`~optuna.FrozenTrial`.</span>
<span class="sd">            Please note that this is not a ``callbacks`` argument of `lightgbm.train()`_ .</span>

<span class="sd">        model_dir:</span>
<span class="sd">            A directory to save boosters. By default, it is set to :obj:`None` and no boosters are</span>
<span class="sd">            saved. Please set shared directory (e.g., directories on NFS) if you want to access</span>
<span class="sd">            :meth:`~optuna.integration.LightGBMTuner.get_best_booster` in distributed environments.</span>
<span class="sd">            Otherwise, it may raise :obj:`ValueError`. If the directory does not exist, it will be</span>
<span class="sd">            created. The filenames of the boosters will be ``{model_dir}/{trial_number}.pkl``</span>
<span class="sd">            (e.g., ``./boosters/0.pkl``).</span>

<span class="sd">    .. _lightgbm.train(): https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.train.html</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">params</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">],</span>
        <span class="n">train_set</span><span class="p">:</span> <span class="s2">&quot;lgb.Dataset&quot;</span><span class="p">,</span>
        <span class="n">num_boost_round</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1000</span><span class="p">,</span>
        <span class="n">valid_sets</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="s2">&quot;VALID_SET_TYPE&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">valid_names</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Any</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">fobj</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Callable</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="n">Any</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">feval</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Callable</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="n">Any</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">feature_name</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">&quot;auto&quot;</span><span class="p">,</span>
        <span class="n">categorical_feature</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">&quot;auto&quot;</span><span class="p">,</span>
        <span class="n">early_stopping_rounds</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">evals_result</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Dict</span><span class="p">[</span><span class="n">Any</span><span class="p">,</span> <span class="n">Any</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">verbose_eval</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Union</span><span class="p">[</span><span class="nb">bool</span><span class="p">,</span> <span class="nb">int</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span>
        <span class="n">learning_rates</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="nb">float</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">keep_training_booster</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">bool</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
        <span class="n">callbacks</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">Callable</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="n">Any</span><span class="p">]]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">time_budget</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">sample_size</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">study</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">optuna</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">Study</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">optuna_callbacks</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">Callable</span><span class="p">[[</span><span class="n">Study</span><span class="p">,</span> <span class="n">FrozenTrial</span><span class="p">],</span> <span class="kc">None</span><span class="p">]]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">model_dir</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">verbosity</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">LightGBMTuner</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
            <span class="n">params</span><span class="p">,</span>
            <span class="n">train_set</span><span class="p">,</span>
            <span class="n">num_boost_round</span><span class="o">=</span><span class="n">num_boost_round</span><span class="p">,</span>
            <span class="n">fobj</span><span class="o">=</span><span class="n">fobj</span><span class="p">,</span>
            <span class="n">feval</span><span class="o">=</span><span class="n">feval</span><span class="p">,</span>
            <span class="n">feature_name</span><span class="o">=</span><span class="n">feature_name</span><span class="p">,</span>
            <span class="n">categorical_feature</span><span class="o">=</span><span class="n">categorical_feature</span><span class="p">,</span>
            <span class="n">early_stopping_rounds</span><span class="o">=</span><span class="n">early_stopping_rounds</span><span class="p">,</span>
            <span class="n">verbose_eval</span><span class="o">=</span><span class="n">verbose_eval</span><span class="p">,</span>
            <span class="n">callbacks</span><span class="o">=</span><span class="n">callbacks</span><span class="p">,</span>
            <span class="n">time_budget</span><span class="o">=</span><span class="n">time_budget</span><span class="p">,</span>
            <span class="n">sample_size</span><span class="o">=</span><span class="n">sample_size</span><span class="p">,</span>
            <span class="n">study</span><span class="o">=</span><span class="n">study</span><span class="p">,</span>
            <span class="n">optuna_callbacks</span><span class="o">=</span><span class="n">optuna_callbacks</span><span class="p">,</span>
            <span class="n">verbosity</span><span class="o">=</span><span class="n">verbosity</span><span class="p">,</span>
        <span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">[</span><span class="s2">&quot;valid_sets&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">valid_sets</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">[</span><span class="s2">&quot;valid_names&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">valid_names</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">[</span><span class="s2">&quot;evals_result&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">evals_result</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">[</span><span class="s2">&quot;learning_rates&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">learning_rates</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">[</span><span class="s2">&quot;keep_training_booster&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">keep_training_booster</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_best_booster_with_trial_number</span> <span class="o">=</span> <span class="kc">None</span>  <span class="c1"># type: Optional[Tuple[lgb.Booster, int]]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_model_dir</span> <span class="o">=</span> <span class="n">model_dir</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_model_dir</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_model_dir</span><span class="p">):</span>
            <span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_model_dir</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">valid_sets</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;`valid_sets` is required.&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">best_booster</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;lgb.Booster&quot;</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Return the best booster.</span>

<span class="sd">        .. deprecated:: 1.4.0</span>
<span class="sd">            Please get the best booster via</span>
<span class="sd">            :class:`~optuna.integration.lightgbm.LightGBMTuner.get_best_booster` instead.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
            <span class="s2">&quot;The `best_booster` attribute is deprecated. Please use `get_best_booster` instead.&quot;</span><span class="p">,</span>
            <span class="ne">DeprecationWarning</span><span class="p">,</span>
        <span class="p">)</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_best_booster</span><span class="p">()</span>

<div class="viewcode-block" id="LightGBMTuner.get_best_booster"><a class="viewcode-back" href="../../../../reference/integration.html#optuna.integration.lightgbm.LightGBMTuner.get_best_booster">[文档]</a>    <span class="k">def</span> <span class="nf">get_best_booster</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;lgb.Booster&quot;</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Return the best booster.</span>

<span class="sd">        If the best booster cannot be found, :class:`ValueError` will be raised. To prevent the</span>
<span class="sd">        errors, please save boosters by specifying the ``model_dir`` arguments of</span>
<span class="sd">        :meth:`~optuna.integration.lightgbm.LightGBMTuner.__init__` when you resume tuning</span>
<span class="sd">        or you run tuning in parallel.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_best_booster_with_trial_number</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_best_booster_with_trial_number</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">best_trial</span><span class="o">.</span><span class="n">number</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_best_booster_with_trial_number</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">trials</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The best booster is not available because no trials completed.&quot;</span><span class="p">)</span>

        <span class="c1"># The best booster exists, but this instance does not have it.</span>
        <span class="c1"># This may be due to resuming or parallelization.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_model_dir</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The best booster cannot be found. It may be found in the other processes due to &quot;</span>
                <span class="s2">&quot;resuming or distributed computing. Please set the `model_dir` argument of &quot;</span>
                <span class="s2">&quot;`LightGBMTuner.__init__` and make sure that boosters are shared with all &quot;</span>
                <span class="s2">&quot;processes.&quot;</span>
            <span class="p">)</span>

        <span class="n">best_trial</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">best_trial</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_model_dir</span><span class="p">,</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">.pkl&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">best_trial</span><span class="o">.</span><span class="n">number</span><span class="p">))</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The best booster cannot be found in </span><span class="si">{}</span><span class="s2">. If you execute `LightGBMTuner` in &quot;</span>
                <span class="s2">&quot;distributed environment, please use network file system (e.g., NFS) to share &quot;</span>
                <span class="s2">&quot;models with multiple workers.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_model_dir</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="s2">&quot;rb&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fin</span><span class="p">:</span>
            <span class="n">booster</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">fin</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">booster</span></div>

    <span class="k">def</span> <span class="nf">_tune_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">target_param_names</span><span class="p">,</span> <span class="n">n_trials</span><span class="p">,</span> <span class="n">sampler</span><span class="p">,</span> <span class="n">step_name</span><span class="p">):</span>
        <span class="c1"># type: (List[str], int, optuna.samplers.BaseSampler, str) -&gt; _OptunaObjective</span>

        <span class="n">objective</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">LightGBMTuner</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">_tune_params</span><span class="p">(</span>
            <span class="n">target_param_names</span><span class="p">,</span> <span class="n">n_trials</span><span class="p">,</span> <span class="n">sampler</span><span class="p">,</span> <span class="n">step_name</span>
        <span class="p">)</span>

        <span class="k">if</span> <span class="n">objective</span><span class="o">.</span><span class="n">best_booster_with_trial_number</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_best_booster_with_trial_number</span> <span class="o">=</span> <span class="n">objective</span><span class="o">.</span><span class="n">best_booster_with_trial_number</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_best_params</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">best_params</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">objective</span>

    <span class="k">def</span> <span class="nf">_create_objective</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">target_param_names</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">],</span>
        <span class="n">train_set</span><span class="p">:</span> <span class="s2">&quot;lgb.Dataset&quot;</span><span class="p">,</span>
        <span class="n">step_name</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
        <span class="n">pbar</span><span class="p">:</span> <span class="n">tqdm</span><span class="o">.</span><span class="n">tqdm</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">_OptunaObjective</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">_OptunaObjective</span><span class="p">(</span>
            <span class="n">target_param_names</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="p">,</span>
            <span class="n">train_set</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">best_score</span><span class="p">,</span>
            <span class="n">step_name</span><span class="o">=</span><span class="n">step_name</span><span class="p">,</span>
            <span class="n">model_dir</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_model_dir</span><span class="p">,</span>
            <span class="n">pbar</span><span class="o">=</span><span class="n">pbar</span><span class="p">,</span>
        <span class="p">)</span></div>


<div class="viewcode-block" id="LightGBMTunerCV"><a class="viewcode-back" href="../../../../reference/integration.html#optuna.integration.lightgbm.LightGBMTunerCV">[文档]</a><span class="nd">@experimental</span><span class="p">(</span><span class="s2">&quot;1.5.0&quot;</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">LightGBMTunerCV</span><span class="p">(</span><span class="n">_LightGBMBaseTuner</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Hyperparameter tuner for LightGBM with cross-validation.</span>

<span class="sd">    It employs the same stepwise approach as</span>
<span class="sd">    :class:`~optuna.integration.lightgbm.LightGBMTuner`.</span>
<span class="sd">    :class:`~optuna.integration.lightgbm.LightGBMTunerCV` invokes `lightgbm.cv()`_ to train</span>
<span class="sd">    and validate boosters while :class:`~optuna.integration.lightgbm.LightGBMTuner` invokes</span>
<span class="sd">    `lightgbm.train()`_. See</span>
<span class="sd">    `a simple example &lt;https://github.com/optuna/optuna/blob/master/examples/lightgbm_tuner_cv.</span>
<span class="sd">    py&gt;`_ which optimizes the validation log loss of cancer detection.</span>

<span class="sd">    Arguments and keyword arguments for `lightgbm.cv()`_ can be passed except</span>
<span class="sd">    ``metrics``, ``init_model`` and ``eval_train_metric``.</span>
<span class="sd">    The arguments that only :class:`~optuna.integration.lightgbm.LightGBMTunerCV` has are</span>
<span class="sd">    listed below:</span>

<span class="sd">    Args:</span>
<span class="sd">        time_budget:</span>
<span class="sd">            A time budget for parameter tuning in seconds.</span>

<span class="sd">        study:</span>
<span class="sd">            A :class:`~optuna.study.Study` instance to store optimization results. The</span>
<span class="sd">            :class:`~optuna.trial.Trial` instances in it has the following user attributes:</span>
<span class="sd">            ``elapsed_secs`` is the elapsed time since the optimization starts.</span>
<span class="sd">            ``average_iteration_time`` is the average time of iteration to train the booster</span>
<span class="sd">            model in the trial. ``lgbm_params`` is a JSON-serialized dictionary of LightGBM</span>
<span class="sd">            parameters used in the trial.</span>

<span class="sd">        optuna_callbacks:</span>
<span class="sd">            List of Optuna callback functions that are invoked at the end of each trial.</span>
<span class="sd">            Each function must accept two parameters with the following types in this order:</span>
<span class="sd">            :class:`~optuna.study.Study` and :class:`~optuna.FrozenTrial`.</span>
<span class="sd">            Please note that this is not a ``callbacks`` argument of `lightgbm.train()`_ .</span>

<span class="sd">    .. _lightgbm.train(): https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.train.html</span>
<span class="sd">    .. _lightgbm.cv(): https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.cv.html</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">params</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">],</span>
        <span class="n">train_set</span><span class="p">:</span> <span class="s2">&quot;lgb.Dataset&quot;</span><span class="p">,</span>
        <span class="n">num_boost_round</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1000</span><span class="p">,</span>
        <span class="n">folds</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span>
            <span class="n">Union</span><span class="p">[</span>
                <span class="n">Generator</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">int</span><span class="p">],</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">],</span>
                <span class="n">Iterator</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">int</span><span class="p">]],</span>
                <span class="s2">&quot;BaseCrossValidator&quot;</span><span class="p">,</span>
            <span class="p">]</span>
        <span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">nfold</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">5</span><span class="p">,</span>
        <span class="n">stratified</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span>
        <span class="n">shuffle</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span>
        <span class="n">fobj</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Callable</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="n">Any</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">feval</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Callable</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="n">Any</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">feature_name</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">&quot;auto&quot;</span><span class="p">,</span>
        <span class="n">categorical_feature</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">&quot;auto&quot;</span><span class="p">,</span>
        <span class="n">early_stopping_rounds</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">fpreproc</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Callable</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="n">Any</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">verbose_eval</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Union</span><span class="p">[</span><span class="nb">bool</span><span class="p">,</span> <span class="nb">int</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span>
        <span class="n">show_stdv</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span>
        <span class="n">seed</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
        <span class="n">callbacks</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">Callable</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="n">Any</span><span class="p">]]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">time_budget</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">sample_size</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">study</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">optuna</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">Study</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">optuna_callbacks</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">Callable</span><span class="p">[[</span><span class="n">Study</span><span class="p">,</span> <span class="n">FrozenTrial</span><span class="p">],</span> <span class="kc">None</span><span class="p">]]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">verbosity</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">LightGBMTunerCV</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
            <span class="n">params</span><span class="p">,</span>
            <span class="n">train_set</span><span class="p">,</span>
            <span class="n">num_boost_round</span><span class="p">,</span>
            <span class="n">fobj</span><span class="o">=</span><span class="n">fobj</span><span class="p">,</span>
            <span class="n">feval</span><span class="o">=</span><span class="n">feval</span><span class="p">,</span>
            <span class="n">feature_name</span><span class="o">=</span><span class="n">feature_name</span><span class="p">,</span>
            <span class="n">categorical_feature</span><span class="o">=</span><span class="n">categorical_feature</span><span class="p">,</span>
            <span class="n">early_stopping_rounds</span><span class="o">=</span><span class="n">early_stopping_rounds</span><span class="p">,</span>
            <span class="n">verbose_eval</span><span class="o">=</span><span class="n">verbose_eval</span><span class="p">,</span>
            <span class="n">callbacks</span><span class="o">=</span><span class="n">callbacks</span><span class="p">,</span>
            <span class="n">time_budget</span><span class="o">=</span><span class="n">time_budget</span><span class="p">,</span>
            <span class="n">sample_size</span><span class="o">=</span><span class="n">sample_size</span><span class="p">,</span>
            <span class="n">study</span><span class="o">=</span><span class="n">study</span><span class="p">,</span>
            <span class="n">optuna_callbacks</span><span class="o">=</span><span class="n">optuna_callbacks</span><span class="p">,</span>
            <span class="n">verbosity</span><span class="o">=</span><span class="n">verbosity</span><span class="p">,</span>
        <span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">[</span><span class="s2">&quot;folds&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">folds</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">[</span><span class="s2">&quot;nfold&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">nfold</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">[</span><span class="s2">&quot;stratified&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">stratified</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">[</span><span class="s2">&quot;shuffle&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">shuffle</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">[</span><span class="s2">&quot;show_stdv&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">show_stdv</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">[</span><span class="s2">&quot;seed&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">seed</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">[</span><span class="s2">&quot;fpreproc&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">fpreproc</span>

    <span class="k">def</span> <span class="nf">_create_objective</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">target_param_names</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">],</span>
        <span class="n">train_set</span><span class="p">:</span> <span class="s2">&quot;lgb.Dataset&quot;</span><span class="p">,</span>
        <span class="n">step_name</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
        <span class="n">pbar</span><span class="p">:</span> <span class="n">tqdm</span><span class="o">.</span><span class="n">tqdm</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">_OptunaObjective</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">_OptunaObjectiveCV</span><span class="p">(</span>
            <span class="n">target_param_names</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_params</span><span class="p">,</span>
            <span class="n">train_set</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lgbm_kwargs</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">best_score</span><span class="p">,</span>
            <span class="n">step_name</span><span class="o">=</span><span class="n">step_name</span><span class="p">,</span>
            <span class="n">pbar</span><span class="o">=</span><span class="n">pbar</span><span class="p">,</span>
        <span class="p">)</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Optuna Contributors.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
    <a href="../../../../privacy.html">Privacy Policy</a>.
     


</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-55135190-8', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>